#include <linked_list.h>
LLIST *llist_get_first(LLIST *tqueue){
	if (tqueue == 0)
		return 0;
	return tqueue->next;
}

LLIST *llist_get_last(LLIST *tqueue){
	if (tqueue == 0)
		return 0;
	return tqueue->prev;
}

LLIST *llist_remove_last(LLIST *tqueue){
	LLIST *result;
	if (tqueue == 0)
		return 0;
	result = tqueue->prev;	
	tqueue->prev = tqueue->prev->prev;
	tqueue->prev->next = tqueue;
	return result;
}

LLIST *llist_remove_first(LLIST *tqueue){
	LLIST *result;
	if (tqueue == 0)
		return 0;
	result = tqueue->next;	
	tqueue->next = tqueue->next->next;
	tqueue->next->prev = tqueue;
	return result;
}

void llist_add_end(LLIST *tqueue,LLIST *entry){
	if (tqueue == 0)
		return;
	entry->prev = tqueue->prev;
	entry->next = tqueue;
	tqueue->prev->next = entry;
	tqueue->prev = entry;
}

void llist_unlink(LLIST *entry){
	entry->prev->next = entry->next;
	entry->next->prev = entry->prev;
	entry->next = 0;
	entry->prev = 0;
}

void llist_create(LLIST *tail){
	tail->next = tail;
	tail->prev = tail;
	tail->value = 0;
}

int llist_size(LLIST *tqueue){
	int size = 0;
	LLIST *work = tqueue->next;
	for (;;){
		if (tqueue == work)
			break;
		size++;
		work = work->next;
	}
	return size;
}

LLIST * kllist_get(LLIST *list,uint32_t index){
	LLIST *current_ll = list->next;
	if (current_ll == list)
		return 0;
	for (uint32_t current = 0;1;current++){
		if (current == index)	
			return current_ll;
		if (current_ll->next == list)
			return 0;
		current_ll = current_ll->next;
	}
}


ILLIST *illist_get(ILLIST *list,int index){
	ILLIST *current_ll = list->next;
	if (current_ll == list)
		return 0;
	if (current_ll == 0)
		return 0;
	for (;;){
		if (current_ll->index == index)	
			return current_ll;
		if (current_ll == 0)
			return 0;
		if (current_ll->next == list)
			return 0;
		current_ll = current_ll->next;
	}
}

